package com.jucai.pppeiyouclient.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.base.QuickFragmentAdapter;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.ScreenUtil;
import com.jucai.pppeiyouclient.view.fragment.HistoricalOrdersFragment;
import com.jucai.pppeiyouclient.view.fragment.NotStartedOrdersFragment;
import com.jucai.pppeiyouclient.view.fragment.PendingOrdersFragment;
import com.jucai.pppeiyouclient.view.fragment.RunningOrdersFragment;

import java.util.ArrayList;
import java.util.List;

public class MyOrdersActivity extends BaseActivity {
    private TabLayout myOrderTab;
    private ViewPager myOrderVp;
    private List<Fragment> fragments = new ArrayList<>();
    private QuickFragmentAdapter<Fragment> quickAdapter;
    private String[] titles = new String[]{"待处理订单", "未开始订单", "进行中订单", "历史订单"};
    private int pagePosition;
    private LinearLayout linearLayout;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_orders);
        fragments.add(new PendingOrdersFragment());
        fragments.add(new NotStartedOrdersFragment());
        fragments.add(new RunningOrdersFragment());
        fragments.add(new HistoricalOrdersFragment());

        quickAdapter = new QuickFragmentAdapter<>(getSupportFragmentManager(), fragments, titles);
        ((TextView) findViewById(R.id.common_title_text)).setText("PP优约");
        myOrderTab = (TabLayout) findViewById(R.id.my_order_tab);
        myOrderVp = (ViewPager) findViewById(R.id.my_order_vp);
        myOrderVp.setAdapter(quickAdapter);
        myOrderTab.setupWithViewPager(myOrderVp);
        myOrderVp.setOffscreenPageLimit(4);

        linearLayout = (LinearLayout) myOrderTab.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.shape_vertical_divider_gray));
        linearLayout.setDividerPadding(ScreenUtil.dip2px(MyOrdersActivity.this, 15));
    }

    @Override
    protected void initData() {

        pagePosition = getIntent().getIntExtra("page_position", 0);
        switch (pagePosition) {
            case 0:
                myOrderVp.setCurrentItem(0);
                break;
            case 1:
                myOrderVp.setCurrentItem(1);
                break;
            case 2:
                myOrderVp.setCurrentItem(2);
                break;
            case 3:
                myOrderVp.setCurrentItem(3);
                break;
            default:
                break;
        }
    }

    @Override
    protected void loadNetData() {

    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
    }

    public static void startMyOrders(Context context, int pagePosition) {
        Intent intent = new Intent(context, MyOrdersActivity.class);
        intent.putExtra("page_position", pagePosition);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(MyOrdersActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;
        }
    }
}
